﻿using Microsoft.AspNetCore.Mvc;
using test;
using test.Models;

namespace Common.Function
{
    public class CommonFunction
    {
        public static string GetColumnName(hongjiContext _db, int ColumnID)
        {
            var Column = _db.Columnmanages.Where(x => x.Id == ColumnID).FirstOrDefault();
            if (Column == null)
            {
                return "Top Level";
            }
            else
            {
                return Column.ColumnName;
            }
        }
        public static List<Column> GetColumnInfo(hongjiContext _db, int ID)
        {
            List<Column> CL = new();

            var ColumnList = _db.Columnmanages.Where(x => x.ColumnId == ID).ToList();
            if (ColumnList.Any())
            {
                foreach(var cl in ColumnList)
                {
                    Column C = new()
                    {
                        ID = cl.Id,
                        ColumnName = cl.ColumnName,
                        ColumnArray = GetColumnInfo(_db, cl.Id)
                    };
                    CL.Add(C);
                }
            }
            return CL;

        }

        public static List<int> GetSubID(hongjiContext _db, int ID)
        {
            List<int> ItemList = new();

            var ColumnInfo = _db.Columnmanages.Where(x => x.ColumnId == ID).Select(x => x.Id).ToList();
            if (ColumnInfo.Any())
            {
                foreach(int i in ColumnInfo)
                {
                    ItemList.AddRange(GetSubID(_db, i));
                }
                return ItemList;
            }
            else
            {
                ItemList.Add(ID);
                return ItemList;
            }

        }

    }
}
